﻿// main.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#define NOMINMAX
#include <CEGUI/RendererModules/OpenGL/GL3Renderer.h>
#include <CEGUI/CEGUI.h>
#include <CEGUI/DefaultResourceProvider.h>

#include <osgViewer/viewer>
#include <osgEarth/Notify>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/Threading>
#include <osgEarth/ShaderGenerator>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgUtil/Optimizer>
#include <iostream>
#include <osgEarth/Metrics>
#include "osgEarth/GDAL";
using namespace osgEarth;
using namespace osgEarth::Util;
#include <iostream>
//using namespace std;
//SKY
#include "osgEarthDrivers\sky_simple\SimpleSkyNode"//for the sky
#include "osgEarthDrivers\sky_simple\SimpleSkyOptions"//for the sky

using namespace CEGUI;

#include "ceguidrawable.h"
#include "hubcamera.h"

int main()
{
	osgEarth::initialize();
	//create a viewer
	osgViewer::Viewer viewer;
	viewer.setUpViewInWindow(0, 100, 800, 600);
	viewer.setReleaseContextAtEndOfFrameHint(false);
	//set camera manipulator
	//osgGA::TrackballManipulator* mp = new osgGA::TrackballManipulator;
	EarthManipulator* mp = new EarthManipulator;
	viewer.setCameraManipulator(mp);
	osgEarth::Map* map = new osgEarth::Map();

	//layer
	GDALImageLayer* layer1 = new GDALImageLayer();
	layer1->setURL(osgEarth::URI("E:/coding/osgearth2023/osgearth3.4/data/world.tif"));
	map->addLayer(layer1);

	//elevation
	GDALElevationLayer* elev = new GDALElevationLayer();
	elev->setURL(osgEarth::URI("E:/coding/osgearth2023/osgearth3.4/data/dem_cog_0.0833.tif"));
	map->addLayer(elev);

	osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);

	osg::ref_ptr<osg::Geode> geodeGui = new osg::Geode();
	geodeGui->setCullingActive(false);
	CEGUIDrawable* guiDrawble = new CEGUIDrawable;
	geodeGui->addDrawable( guiDrawble );
	geodeGui->getOrCreateStateSet()->setAttributeAndModes(new osg::BlendFunc);
	geodeGui->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

	osg::Group* rootNode = new osg::Group;
	osg::ref_ptr<osg::Camera> hubCamera = HubCamera::createHubCamera(0, 800, 0, 600);
	hubCamera->setAllowEventFocus(true);
	hubCamera->addChild(geodeGui);

	rootNode->addChild(hubCamera.get());
	rootNode->addChild(mapNode.get());
	viewer.setSceneData(rootNode);
	viewer.addEventHandler(new CEGUIEventHandler(hubCamera.get()));
	viewer.addEventHandler(new osgViewer::WindowSizeHandler);
	viewer.addEventHandler(new osgViewer::StatsHandler);
	Metrics::run(viewer);
	CEGUI::System::destroy();
	//CEGUI::OpenGL3Renderer::destroy(...); //??
	return 0;
}